<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-button v-waves class="filter-item" @click="handleCreate">
        添加公告
      </el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="标题" prop="title" width="300px" />
      <el-table-column label="详情" prop="content" />
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            plain
            type="primary"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            plain
            type="danger"
            @click="handleDelete(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="type == 0 ? '添加公告' : '编辑公告'"
      :visible.sync="dialogVisible"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="40%"
      @closed="dialogClosed"
    >
      <el-form
        ref="postForm"
        :model="form"
        :rules="rules"
        class="form-container"
      >
        <div class="createPost-main-container">
          <el-form-item label-width="120px" label="标题:" prop="title">
            <el-input v-model="form.title" class="input-spin" />
          </el-form-item>
          <el-form-item label-width="120px" label="详情:" prop="content">
            <el-input
              v-model.trim="form.content"
              type="textarea"
              :autosize="{ minRows: 4 }"
            />
          </el-form-item>
        </div>
      </el-form>
      <div style="margin-top: 40px; text-align: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="handleToggleEdit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getNotice, addNotice, updateNotice, deleteNotice } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  directives: {
    waves
  },
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      listQuery: {
        page: 1,
        per_page: 20
      },
      tableKey: 0,
      listLoading: false,
      list: [],
      dialogVisible: false,
      type: 0,
      form: {
        id: '',
        title: undefined,
        content: undefined
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: '请输入详情',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dialogClosed() {
      this.form = {
        id: undefined,
        title: undefined,
        content: undefined
      }
      this.$refs['postForm'].resetFields()
    },
    getList() {
      this.listLoading = true
      getNotice(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    handleCreate() {
      this.type = 0

      this.form.id = ''
      this.form.title = ''
      this.form.content = ''
      this.dialogVisible = true
    },
    handleUpdate(row) {
      this.type = 1
      this.form = Object.assign({}, row)

      this.dialogVisible = true
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.listLoading = true
          deleteNotice(id)
            .then(() => {
              this.$notify({
                title: '提示',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
            .catch(() => {
              this.listLoading = false
            })
        })
        .catch(() => {})
    },
    handleToggleEdit() {
      this.$refs.postForm.validate(async(valid, object) => {
        if (valid) {
          if (this.type == 0) {
            addNotice(this.form).then(() => {
              this.$notify({
                title: '提示',
                message: '新增成功',
                type: 'success',
                duration: 2000
              })
              this.dialogVisible = false
              this.getList()
            })
          } else {
            const params = {
              title: this.form.title,
              content: this.form.content
            }
            updateNotice(this.form['id'], params).then(() => {
              this.$notify({
                title: '提示',
                message: '编辑成功',
                type: 'success',
                duration: 2000
              })
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
